﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace AmmoTracker
{
    public class SettingsModel
    {
        public int positionX { get; set; }
        public int positionY { get; set; }
        public int textSize { get; set; }
        public int textColourRed { get; set; }
        public int textColourGreen { get; set; }
        public int textColourBlue { get; set; }
        public int textColourAlpha { get; set; }
        public int bgColourRed { get; set; }
        public int bgColourGreen { get; set; }
        public int bgColourBlue { get; set; }
        public int bgColourAlpha { get; set; }
        public bool showEmpty { get; set; }
        public bool showSatchelCounts { get; set; }
        //public bool showSackCounts { get; set; }

        private static SettingsModel _settingsModel = null;

        SettingsModel()
        {
        }

        private void LoadSettings()
        {
            System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();

            positionX = getIntSetting(reader, "AmmoCounter.positionX", 550);
            positionY = getIntSetting(reader, "AmmoCounter.positionY", 30);
            textSize = getIntSetting(reader, "AmmoCounter.textSize", 8);
            textColourRed = getIntSetting(reader, "AmmoCounter.textColourRed", 255);
            textColourGreen = getIntSetting(reader, "AmmoCounter.textColourGreen", 255);
            textColourBlue = getIntSetting(reader, "AmmoCounter.textColourBlue", 255);
            textColourAlpha = getIntSetting(reader, "AmmoCounter.textColourAlpha", 255);
            bgColourRed = getIntSetting(reader, "AmmoCounter.bgColourRed", 0);
            bgColourGreen = getIntSetting(reader, "AmmoCounter.bgColourGreen", 0);
            bgColourBlue = getIntSetting(reader, "AmmoCounter.bgColourBlue", 0);
            bgColourAlpha = getIntSetting(reader, "AmmoCounter.bgColourAlpha", 192);
            showEmpty = getBoolSetting(reader, "AmmoCounter.showEmpty", true);
            showSatchelCounts = getBoolSetting(reader, "AmmoCounter.showSatchelCounts", false);
            //showSackCounts = getBoolSetting(reader, "AmmoCounter.showSackCounts", false);
        }


        public void SaveSettings()
        {
            System.Configuration.Configuration cfg =
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            KeyValueConfigurationCollection kv = cfg.AppSettings.Settings;

            updateKV(kv, "AmmoCounter.positionX", positionX.ToString());
            updateKV(kv, "AmmoCounter.positionY", positionY.ToString());
            updateKV(kv, "AmmoCounter.textSize", textSize.ToString());
            updateKV(kv, "AmmoCounter.textColourRed", textColourRed.ToString());
            updateKV(kv, "AmmoCounter.textColourGreen", textColourGreen.ToString());
            updateKV(kv, "AmmoCounter.textColourBlue", textColourBlue.ToString());
            updateKV(kv, "AmmoCounter.textColourAlpha", textColourAlpha.ToString());
            updateKV(kv, "AmmoCounter.bgColourRed", bgColourRed.ToString());
            updateKV(kv, "AmmoCounter.bgColourGreen", bgColourGreen.ToString());
            updateKV(kv, "AmmoCounter.bgColourBlue", bgColourBlue.ToString());
            updateKV(kv, "AmmoCounter.bgColourAlpha", bgColourAlpha.ToString());
            updateKV(kv, "AmmoCounter.showEmpty", TF10(showEmpty));
            updateKV(kv, "AmmoCounter.showSatchelCounts", TF10(showSatchelCounts));
            //updateKV(kv, "AmmoCounter.showSackCounts", TF10(showSackCounts));

            cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void updateKV(KeyValueConfigurationCollection kv, string key_name, string value_string)
        {
            kv.Remove(key_name);
            kv.Add(key_name, value_string);
        }

        public static SettingsModel getInstance()
        {
            if (_settingsModel == null)
            {
                _settingsModel = new SettingsModel();

                _settingsModel.LoadSettings();
            }


            return _settingsModel;
        }

        private string TF10(bool v)
        {
            return v ? "1" : "0";
        }

        bool getBoolSetting(System.Configuration.AppSettingsReader reader, string valueName, bool default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(int));
                if (o == null)
                    return default_value;
                return 0 != (int)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        byte getByteSetting(System.Configuration.AppSettingsReader reader, string valueName, byte default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(int));
                if (o == null)
                    return default_value;
                return (byte)(int)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        float getFloatSetting(System.Configuration.AppSettingsReader reader, string valueName, float default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(float));
                if (o == null)
                    return default_value;
                return (float)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        int getIntSetting(System.Configuration.AppSettingsReader reader, string valueName, int default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(int));
                if (o == null)
                    return default_value;
                return (int)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        string getStringSetting(System.Configuration.AppSettingsReader reader, string valueName, string default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(string));
                if (o == null)
                    return default_value;
                return (string)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }
    }

}
